﻿using PublicLib.Def;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel.Web;
using System.Text;
using System.Web;

namespace PublicLib.Lib.Help
{
    public static class StringStreamHelp
    {
        public static Stream ToStream(this string self)
        {
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms, PublicDef.PageEncoding);
            sw.AutoFlush = true;
            sw.Write(self);
            ms.Position = 0;
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
            return ms;
        }
        public static string ToString(this Stream inputStream, bool autoClose = false)
        {
            using (StreamReader sr = new StreamReader(inputStream, PublicDef.PageEncoding))
            {
                string res = sr.ReadToEnd();
                if (autoClose)
                { 
                    inputStream.Close();
                    inputStream.Dispose();
                }
                return res;
            }
        }
    }
}